//
//  TestOptionTableViewCell.m
//  Learn
//
//  Created by 迟建国 on 2024/4/2.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "TestOptionTableViewCell.h"

@implementation TestOptionTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.readBtn.layer.cornerRadius = 17;
    self.readBtn.layer.masksToBounds = YES;
    [self.recordViewPlay setImage:[UIImage imageNamed:@"ic_audio_play_stop"] forState:UIControlStateNormal];
    [self.recordViewPlay setImage:[UIImage imageNamed:@"ic_audio_play_play"] forState:UIControlStateSelected];
    [self.myRecordViewPlay setImage:[UIImage imageNamed:@"ic_audio_play_stop"] forState:UIControlStateNormal];
    [self.myRecordViewPlay setImage:[UIImage imageNamed:@"ic_audio_play_play"] forState:UIControlStateSelected];
    
    [self.recordViewContent addSubview:self.recordView];
    [self.myRecordViewContent addSubview:self.myRecordView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetNoPlay:) name:@"playerStatueChange" object:nil];
    // Initialization code
}
-(void)resetNoPlay:(NSNotification *)notic
{
    if (notic.object != self) {
        self.recordViewPlay.selected = self.myRecordViewPlay.selected = NO;
        self.viewModel.audioPlay = NO;
        self.viewModel.myAudioPlay = NO;
    }
}
- (AudioPlayProgressView *)recordView
{
    if (!_recordView) {
        _recordView = [[AudioPlayProgressView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth - 20 - 38 - 32, 38) Progress:0.8];
    }
    return _recordView;
}
- (AudioPlayProgressView *)myRecordView
{
    if (!_myRecordView) {
        _myRecordView = [[AudioPlayProgressView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth -20 - 38 - 32, 38) Progress:0.8];
    }
    return _myRecordView;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setViewModel:(TestSubCellViewModel *)viewModel
{
    _viewModel = viewModel;
    self.questionModel = viewModel.question;
    self.textLA.text = self.questionModel.trunk.body;
    self.recordViewPlay.selected = viewModel.audioPlay;
    self.myRecordViewPlay.selected = viewModel.myAudioPlay;
    self.recordView.progress = viewModel.audioProgress;
    self.imgView.hidden = YES;
    if (viewModel.img) {
        self.imgView.hidden = NO;
        self.imgView.image = viewModel.img;
        self.imageH.constant = viewModel.img.size.height/viewModel.img.size.width*(SCREEN_W - 20);
    }
    self.myRecordView.hidden = self.myTitleLab.hidden = self.myRecordViewPlay.hidden = NO;
    self.recordView.hidden =  self.recordViewPlay.hidden = NO;
    self.recordView.timeLabel.text = [NSString stringWithFormat:@"%@ / %@",[MethodTool getPlayTimeWithTime:viewModel.audioTimeAll*viewModel.audioProgress],[MethodTool getPlayTimeWithTime:viewModel.audioTimeAll]];
    if (self.questionModel.recordUrl.length != 0) {
        self.myRecordView.progress = viewModel.myAudioProgress;
        if (viewModel.myAudioTimeAll <= 0) {
            dispatch_queue_t t = dispatch_queue_create("my", nil);
            dispatch_async(t, ^{
                [MethodTool getAudioMp3TimeWithUrl:self.questionModel.recordUrl complete:^(double time) {
                    viewModel.myAudioTimeAll = time;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.myRecordView.timeLabel.text = [NSString stringWithFormat:@"%@ / %@",[MethodTool getPlayTimeWithTime:viewModel.myAudioTimeAll*viewModel.myAudioProgress],[MethodTool getPlayTimeWithTime:viewModel.myAudioTimeAll]];
                    });
                }];
            });
        }
        self.myRecordView.timeLabel.text = [NSString stringWithFormat:@"%@ / %@",[MethodTool getPlayTimeWithTime:viewModel.myAudioTimeAll*viewModel.myAudioProgress],[MethodTool getPlayTimeWithTime:viewModel.myAudioTimeAll]];
       
    }else{
        self.myRecordView.hidden = self.myTitleLab.hidden = self.myRecordViewPlay.hidden = YES;
    }
    HSQuestion *testModel = self.questionModel;
    NSString *aUrl = @"";
    if (testModel.trunk.attachments.count != 0) {
        
        HSValueAttachment *k= testModel.trunk.attachments[0];
        for (HSValueAttachment *att in testModel.trunk.attachments) {
            if (att.fileType == 2) {
                k = att;
            }
        }
        aUrl = [NSString stringWithFormat:@"%@%@",self.resmain,k.url];
    }
    if (aUrl.length != 0) {
        self.recordView.progress = viewModel.myAudioProgress;
        if (viewModel.audioTimeAll <= 0) {
            dispatch_queue_t t = dispatch_queue_create("my", nil);
            dispatch_async(t, ^{
                [MethodTool getAudioMp3TimeWithUrl:aUrl complete:^(double time) {
                    viewModel.audioTimeAll = time;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.recordView.timeLabel.text = [NSString stringWithFormat:@"%@ / %@",[MethodTool getPlayTimeWithTime:viewModel.audioTimeAll*viewModel.audioProgress],[MethodTool getPlayTimeWithTime:viewModel.audioTimeAll]];
                    });
                }];
            });
        }
        self.recordView.timeLabel.text = [NSString stringWithFormat:@"%@ / %@",[MethodTool getPlayTimeWithTime:viewModel.audioTimeAll*viewModel.audioProgress],[MethodTool getPlayTimeWithTime:viewModel.audioTimeAll]];
       
    }else{
        self.recordView.hidden =  self.recordViewPlay.hidden = YES;
    }
    
    if (self.viewModel.audioPlay||self.viewModel.myAudioPlay) {
        [PlayManager sharPlayManager].delegate = self;
    }
    
}

- (IBAction)playQuestion:(UIButton *)sender {
    
    HSQuestion *testModel = self.questionModel;
    NSString *aUrl = @"";
    if (testModel.trunk.attachments.count != 0) {
        
        HSValueAttachment *k= testModel.trunk.attachments[0];
        for (HSValueAttachment *att in testModel.trunk.attachments) {
            if (att.fileType == 2) {
                k = att;
            }
        }
        aUrl = [NSString stringWithFormat:@"%@%@",self.resmain,k.url];
    }
    if (IsStrEmpty(aUrl)) return;
    sender.selected = !sender.selected;
    self.viewModel.audioPlay = sender.selected;
    if (sender.selected) {
        [self playWithUrl:aUrl];
    }else{
        [[PlayManager sharPlayManager] pause];
    }
    
    self.myRecordViewPlay.selected = NO;
    self.viewModel.myAudioPlay = NO;
    
}
- (IBAction)playMyAnswer:(UIButton *)sender {
    
    if (IsStrEmpty(self.questionModel.recordUrl)) return;
    sender.selected = !sender.selected;
    self.viewModel.myAudioPlay = sender.selected;
    if (sender.selected) {
        if (!IsStrEmpty(self.questionModel.recordUrl)) {
            [self playWithUrl:self.questionModel.recordUrl];
        }
       
    }else{
        [[PlayManager sharPlayManager] pause];
    }
    
    self.recordViewPlay.selected = NO;
    self.viewModel.audioPlay = NO;
    
}
- (void)playWithUrl:(NSString *)url
{
    [PlayManager sharPlayManager].delegate = self;
    [[PlayManager sharPlayManager] playerWithURL:url];
}

- (void)playerWithPlayer:(AVPlayer *)player progress:(float)progress duration:(float)duration currentTime:(float)currentTime
{
    
    if (self.myRecordViewPlay.selected) {
        self.viewModel.myAudioProgress = progress;
        self.myRecordView.progress = progress;
        self.myRecordView.timeLabel.text = [NSString stringWithFormat:@"%@ / %@",[MethodTool getPlayTimeWithTime:currentTime],[MethodTool getPlayTimeWithTime:duration]];
    }
    
    if (self.recordViewPlay.selected) {
        self.recordView.progress = progress;
        self.viewModel.audioProgress = progress;
        self.recordView.timeLabel.text = [NSString stringWithFormat:@"%@ / %@",[MethodTool getPlayTimeWithTime:currentTime],[MethodTool getPlayTimeWithTime:duration]];
    }
}
- (void)musicFinsh
{
    self.recordViewPlay.selected = self.myRecordViewPlay.selected = NO;
    self.viewModel.audioPlay = NO;
    self.viewModel.myAudioPlay = NO;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
